package com.tedu.weibo.controller;

import com.tedu.weibo.mapper.CommentMapper;
import com.tedu.weibo.pojo.dto.CommentDTO;
import com.tedu.weibo.pojo.entity.Comment;
import com.tedu.weibo.pojo.entity.User;
import com.tedu.weibo.pojo.vo.CommentVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/comment")
@Api(tags = "评论模块")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;

    @ApiOperation(value = "评论功能")
    @PostMapping("/insert")
    public int insert(@RequestBody CommentDTO commentDTO,@ApiIgnore HttpSession session){
        System.out.println("开始处理发表评论");
        System.out.println(commentDTO);
        //验证登录
        User user = (User) session.getAttribute("user");
        if(user==null){
            return 2;
        }
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO,comment);
        comment.setCreated(new Date());
        comment.setUserId(user.getId());
        commentMapper.insertComment(comment);
        return 1;
    }

    @ApiOperation(value = "获取评论列表功能")
    @GetMapping("/selectByWeiboId")
    public List<CommentVO> findByWeiboId(Integer id){
        return commentMapper.selectByWeiboId(id);
    }
}
